angular.module('factory').controller('WelcomeController', function ($scope, $http, $state, $timeout) {

    jQuery('#welcomeDiv').css({
        'position': 'absolute',
        'width': '100%',
        'top': (screen.availHeight / 3) * 2
    });

    jQuery('#welcomeText').css({
        'font-size': (screen.availHeight / 15),
        'text-align': 'center'
    });


    $scope.logindata = {
        username: '',
        password: ''
    };

    var timer = $timeout(function(){
        // 如果存在账号密码于缓存中 那么就自动登录
        if (localStorage.getItem("userInfo")){
            var data = JSON.parse(localStorage.getItem("userInfo"));
            $scope.logindata.username = data.username;
            $scope.logindata.password = data.password;
            $http({
                method: 'POST',
                url: urls.login,
                data: $scope.logindata
            }).success(function (data) {
                if (data == 'failure'){
                    console.log("登录失败！");
                    $state.go('login');
                }

                var username = data.username;
                if (username != undefined && username != null){
                    localStorage.setItem('userInfo', JSON.stringify($scope.logindata));
                    $state.go('home');
                }

            }).error(function () {
                console.log("登录失败！");
            });
        }else{
            console.log("未登录！");
            $state.go('login');
        }
    },2000);

});